package com.qk.controller;


import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import dto.CustomerDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
@GetMapping("/customers")
    public Result list(CustomerDto customerDto){
    log.info("查询客户列表: {}", customerDto);
    PageResult<Customer> pageResult = customerService.listCustomers(customerDto);
    return Result.success(pageResult);
}
/**
 * 添加客户
 */
@PostMapping("/customers")
    public Result addCustomer(@RequestBody Customer customer){
    log.info("添加客户: {}", customer);
    customer.setCreateTime(LocalDateTime.now());
    customer.setUpdateTime(LocalDateTime.now());
    customerService.save(customer);
    return Result.success();
}
/**
 * 根据id查询客户
 */
@GetMapping("/customers/{id}")
    public Result getById(@PathVariable Integer id){
    log.info("查询客户id为{}",id);
    Customer customer = customerService.getCustomerById(id);
    return Result.success(customer);
}
/**
 * 修改客户
 */
@PutMapping("/customers")
    public Result updateCustomer(@RequestBody Customer customer) {
    log.info("更新客户:{}", customer);
    customer.setUpdateTime(LocalDateTime.now());
    customerService.updateById(customer);
    return Result.success();
}




}
